import asyncio
import threading
import uuid

from fastapi import APIRouter, Depends
from app.api.GaiController import generate_document_tasks, tasks_storage, generate_architecture_task, \
    split_task
from app.common.AiUtils import AiCli
from app.common.StandPrompts import create_source_code_prompt
from app.core.response.ResponseResult import ResponseResult
from app.dto.GaiDto import TeDto

#
# client = OpenAI(api_key="sk-98d398b3884f49b6985a5e49f01db404", base_url="https://api.deepseek.com")
#
router = APIRouter(prefix="/ai", tags=["ai操作"])
tem_path = "temp_static"
@router.post("/test", summary="个人信息", description="获取当前登录用户的详细信息")
async def get_user_info(sc:TeDto):
    app_from = await split_task(system_name=sc.system_name,feature_list=sc.feature_list,tech_stack=sc.tech_stack)
    sources_doc_path = f"{tem_path}/111111/{sc.system_name}代码文档.docx"
    with open(sources_doc_path, 'w',encoding="utf-8") as f:
        f.write(app_from)
    return ResponseResult.ok_data(data=app_from)
@router.post("/architecture", summary="生成系统架构", description="读取当前md")
async def get_user_info(sc:TeDto):
    task_id = str(uuid.uuid4())
    # 初始化任务状态
    tasks_storage[task_id] = {
        "status": "processing",
        "data": None,
        "error": None
    }

    # 在后台运行任务
    asyncio.create_task(generate_architecture_task(task_id, sc, tem_path))
    # 立即返回任务ID
    return ResponseResult.ok_data(data={"task_id": task_id})


@router.get("/architecture/task/{task_id}", summary="查询架构生成任务状态")
async def get_task_status(task_id: str):
    task_info = tasks_storage.get(task_id)
    if not task_info:
        return ResponseResult.error(message="任务不存在")
    return ResponseResult.ok_data(data=task_info)

